#!/bin/bash
set -e
set -x
MYDIR="${0%/*}"
cd "$MYDIR/.."
source "recipes/common.sh"


# RECIPE START cmake
# depends: cmake-native

rm -rf build/cmake
mkdir -p build/cmake
cd build/cmake
compiler_sysroot=`${TARGET}-gcc -print-sysroot`
cat > ${TARGET}-toolchain.cmake <<EOF
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR ${TARGET##-*})
set(CMAKE_C_COMPILER ${TARGET}-gcc)
set(CMAKE_CXX_COMPILER ${TARGET}-g++)
set(CMAKE_FIND_ROOT_PATH ${stage}${prefix})

set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
EOF

# do NOT link with openssl and co
unset CPPFLAGS
unset LDFLAGS
# XXX: debugging symbols occupy tons of disk space
env \
	CFLAGS="-O2 -g1 -Wall -fPIC" \
	CXXFLAGS="-O2 -g1 -Wall -fPIC" \
${xtools}${build_prefix}/bin/cmake \
	-DCMAKE_TOOLCHAIN_FILE="${TARGET}-toolchain.cmake" \
	-DCMAKE_BUILD_TYPE=Release \
        -DCMAKE_SKIP_INSTALL_RPATH:BOOL=yes \
        -DCMAKE_INSTALL_PREFIX=${prefix} \
        -DINCLUDE_INSTALL_DIR:PATH=${prefix}/include \
        -DLIB_INSTALL_DIR:PATH=${prefix}/${target_libdir} \
        -DSHARE_INSTALL_PREFIX:PATH=${prefix}/share \
        -DSYSCONF_INSTALL_DIR:PATH=${sysconfdir} \
	-DCMAKE_DATA_DIR=share/cmake \
	-DLIB_SUFFIX=${target_libdir##lib} \
	-DCMAKE_USE_SYSTEM_LIBRARIES=OFF \
	-DCMAKE_USE_OPENSSL=OFF \
	../../cmake

${xtools}${build_prefix}/bin/cmake \
	--build . --verbose --parallel ${JOBS}

env DESTDIR=`pwd`/inst \
${xtools}${build_prefix}/bin/cmake \
	--build . --verbose \
	-t install

cd inst
find "${prefix#/}" | cpio -pumd "$stage"

cd ../../..
rm -rf build/cmake

# RECIPE END cmake

